package cn.felord.common.bean;

import java.io.Serializable;

/**
 * The type Rest body.
 *
 * @param <T> the type parameter
 * @author Dax
 * @since 22 :10  2018/9/9
 */
public class RestBody<T> implements Rest<T>, Serializable {

    private static final long serialVersionUID = -7616216747521482608L;

    private int httpStatus = 200;
    private T payload;
    private String msg = "";
    private String identifier = IdentifierEnum.ENABLE.value();

    /**
     * Gets http status.
     *
     * @return the http status
     */
    public int getHttpStatus() {
        return httpStatus;
    }

    public void setHttpStatus(int httpStatus) {
        this.httpStatus = httpStatus;
    }

    /**
     * Gets payload.
     *
     * @return the payload
     */
    public T getPayload() {
        return payload;
    }

    public void setPayload(T payload) {
        this.payload = payload;
    }

    /**
     * Gets msg.
     *
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * Gets identifier.
     *
     * @return the identifier
     */
    public String getIdentifier() {
        return identifier;
    }

    /**
     * Sets identifier.
     *
     * @param identifier the identifier
     */
    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    /**
     * Ok rest.
     *
     * @return the rest
     */
    public static Rest ok() {
        return new RestBody();
    }

    /**
     * Ok rest.
     *
     * @param msg the msg
     * @return the rest
     */
    public static Rest ok(String msg) {
        Rest restBody = new RestBody();
        restBody.setMsg(msg);
        return restBody;
    }

    /**
     * Ok data rest.
     *
     * @param <T>     the type parameter
     * @param payload the payload
     * @return the rest
     */
    public static <T> Rest<T> okData(T payload) {
        Rest<T> restBody = new RestBody<>();
        restBody.setPayload(payload);
        return restBody;
    }
    public static <T> Rest<T> okData(T payload,String msg) {
        Rest<T> restBody = new RestBody<>();
        restBody.setPayload(payload);
        restBody.setMsg(msg);
        return restBody;
    }
    /**
     * Build rest.
     *
     * @param <T>            the type parameter
     * @param httpStatus     the http status
     * @param payload        the payload
     * @param msg            the msg
     * @param identifierEnum the identifier enum
     * @return the rest
     */
    public static <T> Rest<T> build(int httpStatus, T payload, String msg, IdentifierEnum identifierEnum) {
        Rest<T> restBody = new RestBody<>();
        restBody.setHttpStatus(httpStatus);
        restBody.setPayload(payload);
        restBody.setMsg(msg);
        restBody.setIdentifier(identifierEnum.value());
        return restBody;
    }

    /**
     * Failure rest.
     *
     * @param msg the msg
     * @return the rest
     */
    public static Rest failure(String msg) {
        Rest restBody = new RestBody();
        restBody.setMsg(msg);
        restBody.setIdentifier(IdentifierEnum.DISABLE.value());
        return restBody;
    }

    /**
     * Failure data rest.
     *
     * @param <T>     the type parameter
     * @param payload the payload
     * @param msg     the msg
     * @return the rest
     */
    public static <T> Rest<T> failureData( T payload, String msg) {
        Rest<T> restBody = new RestBody<>();
        restBody.setIdentifier(IdentifierEnum.DISABLE.value());
        restBody.setPayload(payload);
        restBody.setMsg(msg);
        return restBody;
    }

    @Override
    public String toString() {
        return "{" +
                "httpStatus:" + httpStatus +
                ", payload:" + payload +
                ", msg:" + msg +
                '}';
    }
}
